<template>
	<view class="detail-container">
		<view class="detail-card" v-if="reminder">
			<view class="reminder-header" :class="'type-' + reminder.type">
				<view class="reminder-type">
					<text class="type-icon iconfont" :class="getTypeIcon(reminder.type)"></text>
					<text class="type-text">{{ getTypeText(reminder.type) }}</text>
				</view>
				<view class="reminder-status" :class="{ active: reminder.active }">
					{{ reminder.active ? '已启用' : '已禁用' }}
				</view>
			</view>
			
			<view class="reminder-content">
				<view class="reminder-title">{{ reminder.title }}</view>
				
				<view class="info-card">
					<view class="info-item">
						<text class="info-label">
							<text class="iconfont icon-time"></text>
							提醒时间
						</text>
						<text class="info-value">{{ reminder.time }}</text>
					</view>
					
					<view class="info-item">
						<text class="info-label">
							<text class="iconfont icon-repeat"></text>
							重复规则
						</text>
						<text class="info-value">{{ reminder.repeatText || '不重复' }}</text>
					</view>
					
					<view class="info-item" v-if="reminder.desc">
						<text class="info-label">
							<text class="iconfont icon-desc"></text>
							提醒描述
						</text>
						<text class="info-value desc">{{ reminder.desc }}</text>
					</view>
					
					<view class="info-item">
						<text class="info-label">
							<text class="iconfont icon-time"></text>
							创建时间
						</text>
						<text class="info-value">{{ formatDateTime(reminder.createTime) }}</text>
					</view>
					
					<view class="info-item" v-if="reminder.lastTriggered">
						<text class="info-label">
							<text class="iconfont icon-history"></text>
							上次提醒
						</text>
						<text class="info-value">{{ formatDateTime(reminder.lastTriggered) }}</text>
					</view>
				</view>
			</view>
			
			<!-- 操作按钮 -->
			<view class="action-buttons">
				<button class="btn-edit" @tap="editReminder">
					<text class="iconfont icon-edit"></text>
					编辑
				</button>
				<button class="btn-toggle" @tap="toggleStatus" :class="{ active: reminder.active }">
					<text class="iconfont" :class="reminder.active ? 'icon-disable' : 'icon-enable'"></text>
					{{ reminder.active ? '禁用' : '启用' }}
				</button>
				<button class="btn-delete" @tap="confirmDelete">
					<text class="iconfont icon-delete"></text>
					删除
				</button>
			</view>
		</view>
		
		<view class="empty-state" v-else>
			<image src="/static/images/empty-state.png" mode="aspectFit" class="empty-image"></image>
			<text class="empty-text">提醒不存在或已被删除</text>
			<button class="btn-back" @tap="goBack">返回列表</button>
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue'
	import { initReminderData, getReminderDetail, toggleReminderStatus, deleteReminder } from '@/common/api/reminder.js'
	import { formatDate } from '@/common/utils/date.js'
	
	// 当前提醒ID
	const id = ref(null)
	
	// 提醒数据
	const reminder = ref(null)
	
	// 格式化日期时间
	const formatDateTime = (timestamp) => {
		if (!timestamp) return '--'
		const date = new Date(timestamp)
		return formatDate(date, 'YYYY-MM-DD HH:mm')
	}
	
	// 获取提醒类型图标
	const getTypeIcon = (type) => {
		const iconMap = {
			'medicine': 'icon-medicine',
			'bloodSugar': 'icon-blood',
			'exercise': 'icon-exercise',
			'diet': 'icon-diet',
			'other': 'icon-other'
		}
		return iconMap[type] || 'icon-other'
	}
	
	// 获取提醒类型文本
	const getTypeText = (type) => {
		const typeMap = {
			'medicine': '服药',
			'bloodSugar': '血糖检测',
			'exercise': '运动',
			'diet': '饮食',
			'other': '其他'
		}
		return typeMap[type] || '其他'
	}
	
	// 加载提醒详情
	const loadReminderDetail = async () => {
		if (!id.value) return
		
		uni.showLoading({
			title: '加载中...'
		})
		
		try {
			// 确保本地数据已初始化
			initReminderData()
			
			const res = await getReminderDetail(id.value)
			if (res.code === 0 && res.data) {
				reminder.value = res.data
			} else {
				uni.showToast({
					title: '提醒不存在',
					icon: 'none'
				})
				setTimeout(() => {
					goBack()
				}, 1500)
			}
		} catch (e) {
			console.error('获取提醒详情失败:', e)
			uni.showToast({
				title: typeof e === 'string' ? e : '获取提醒详情失败',
				icon: 'none'
			})
		} finally {
			uni.hideLoading()
		}
	}
	
	// 切换提醒状态
	const toggleStatus = async () => {
		if (!reminder.value) return
		
		const newStatus = !reminder.value.active
		
		try {
			const res = await toggleReminderStatus(reminder.value.id, newStatus)
			if (res.code === 0) {
				reminder.value.active = newStatus
				uni.showToast({
					title: newStatus ? '提醒已启用' : '提醒已禁用',
					icon: 'success'
				})
			} else {
				throw new Error(res.message || '操作失败')
			}
		} catch (e) {
			console.error('切换提醒状态失败:', e)
			uni.showToast({
				title: typeof e === 'string' ? e : '操作失败',
				icon: 'none'
			})
		}
	}
	
	// 编辑提醒
	const editReminder = () => {
		if (!reminder.value) return
		uni.navigateTo({
			url: `/pages/reminder/edit?id=${reminder.value.id}`
		})
	}
	
	// 确认删除
	const confirmDelete = () => {
		if (!reminder.value) return
		
		uni.showModal({
			title: '确认删除',
			content: `是否删除提醒"${reminder.value.title}"？`,
			success: async (res) => {
				if (res.confirm) {
					try {
						const result = await deleteReminder(reminder.value.id)
						if (result.code === 0) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							})
							
							setTimeout(() => {
								goBack()
							}, 1500)
						} else {
							throw new Error(result.message || '删除失败')
						}
					} catch (e) {
						uni.showToast({
							title: typeof e === 'string' ? e : '删除失败',
							icon: 'none'
						})
					}
				}
			}
		})
	}
	
	// 返回列表
	const goBack = () => {
		uni.navigateBack()
	}
	
	onMounted(() => {
		const pages = getCurrentPages()
		const page = pages[pages.length - 1]
		
		if (page && page.options) {
			id.value = page.options.id
			loadReminderDetail()
		} else {
			uni.showToast({
				title: '参数错误',
				icon: 'none'
			})
			setTimeout(() => {
				goBack()
			}, 1500)
		}
	})
</script>

<style lang="scss">
	.detail-container {
		background-color: #f5f7fa;
		min-height: 100vh;
		padding: 30rpx;
		
		.detail-card {
			background-color: #fff;
			border-radius: 16rpx;
			box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.08);
			overflow: hidden;
			
			.reminder-header {
				padding: 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				
				&.type-medicine {
					background: linear-gradient(135deg, #5e9dff 0%, #3a7fdb 100%);
				}
				
				&.type-bloodSugar {
					background: linear-gradient(135deg, #ff6b6b 0%, #e04848 100%);
				}
				
				&.type-exercise {
					background: linear-gradient(135deg, #ffaa5e 0%, #ff8c2c 100%);
				}
				
				&.type-diet {
					background: linear-gradient(135deg, #8cc9a1 0%, #5daa7f 100%);
				}
				
				&.type-other {
					background: linear-gradient(135deg, #a18cff 0%, #8269e6 100%);
				}
				
				.reminder-type {
					display: flex;
					align-items: center;
					color: #fff;
					font-size: 28rpx;
					font-weight: bold;
					
					.type-icon {
						font-size: 36rpx;
						margin-right: 12rpx;
					}
				}
				
				.reminder-status {
					font-size: 24rpx;
					padding: 6rpx 16rpx;
					border-radius: 30rpx;
					background-color: rgba(255, 255, 255, 0.2);
					color: #fff;
					
					&.active {
						background-color: rgba(255, 255, 255, 0.3);
					}
				}
			}
			
			.reminder-content {
				padding: 30rpx;
				
				.reminder-title {
					font-size: 40rpx;
					font-weight: bold;
					color: #333;
					margin-bottom: 30rpx;
				}
				
				.info-card {
					background-color: #f9f9f9;
					border-radius: 12rpx;
					padding: 20rpx;
					
					.info-item {
						margin-bottom: 24rpx;
						
						&:last-child {
							margin-bottom: 0;
						}
						
						.info-label {
							display: block;
							font-size: 26rpx;
							color: #666;
							margin-bottom: 8rpx;
							
							.iconfont {
								margin-right: 8rpx;
								color: #8cc9a1;
							}
						}
						
						.info-value {
							font-size: 30rpx;
							color: #333;
							font-weight: 500;
							
							&.desc {
								font-size: 28rpx;
								font-weight: normal;
								color: #555;
								line-height: 1.5;
							}
						}
					}
				}
			}
			
			.action-buttons {
				display: flex;
				padding: 20rpx 30rpx 40rpx;
				
				button {
					flex: 1;
					height: 90rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 30rpx;
					border-radius: 45rpx;
					margin: 0 10rpx;
					box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.05);
					border: none;
					
					.iconfont {
						margin-right: 8rpx;
						font-size: 32rpx;
					}
					
					&.btn-edit {
						background-color: #5e9dff;
						color: #fff;
					}
					
					&.btn-toggle {
						background-color: #ffaa5e;
						color: #fff;
						
						&.active {
							background-color: #999;
						}
					}
					
					&.btn-delete {
						background-color: #ff6b6b;
						color: #fff;
					}
					
					&:active {
						transform: translateY(2rpx);
						box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
					}
				}
			}
		}
		
		.empty-state {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 100rpx 0;
			
			.empty-image {
				width: 240rpx;
				height: 240rpx;
				margin-bottom: 30rpx;
			}
			
			.empty-text {
				font-size: 28rpx;
				color: #999;
				margin-bottom: 50rpx;
			}
			
			.btn-back {
				background: linear-gradient(135deg, #8cc9a1 0%, #5daa7f 100%);
				color: #fff;
				border-radius: 40rpx;
				padding: 20rpx 60rpx;
				font-size: 28rpx;
				box-shadow: 0 8rpx 16rpx rgba(140, 201, 161, 0.2);
				border: none;
			}
		}
	}
</style> 